<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/10/31
 * Time: 1:22 PM
 */

namespace App\Library;


class KdClient
{
    public $EBusinessID = '1396022';
    public $AppKey = 'e324bd29-150b-4ed8-bfe6-4a3e04dce87e';
    public $DataType = '2';

    private $params = [
        'OrderCode'=>'',
        'ShipperCode'=>'',
        'LogisticCode'=>''
    ];

    /**
     * 设置订单号
     * @param $OrderCode
     */
    public function setOrderCode($OrderCode)
    {
        $this->params['OrderCode'] = $OrderCode;
    }

    /**
     * 设置快递编码
     * @param $ShipperCode
     */
    public function setShipperCode($ShipperCode)
    {
        $this->params['ShipperCode'] = $ShipperCode;
    }

    /**
     * 设置快递单号
     * @param $LogisticCode
     */
    public function setLogisticCode($LogisticCode)
    {
        $this->params['LogisticCode'] = $LogisticCode;
    }

    /**
     * Json方式 查询订单物流轨迹
     */
    public function sendRequest(){
        //$requestData= "{'OrderCode':'','ShipperCode':'YTO','LogisticCode':'12345678'}";
        $requestData = json_encode($this->params);
        $url = 'http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx';

        $datas = array(
            'EBusinessID' => $this->EBusinessID,
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData) ,
            'DataType' => $this->DataType,
        );
        $datas['DataSign'] = $this->encrypt($requestData, $this->AppKey);
        $result = $this->curlPost($url, $datas);

        return new KdResponse(json_decode($result, true));
    }

    /**
     * @param $url
     * @param string $data
     * @param int $ssl
     * @param int $timeout
     * @return mixed
     */
    private function curlPost($url, $data='', $ssl=0, $timeout=500){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $ssl);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $ssl);
        //curl_setopt($curl, CURLOPT_HTTPHEADER, ['application/x-www-form-urlencoded','charset=utf-8']);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

    /**
     * 电商Sign签名生成
     * @param data 内容
     * @param appkey Appkey
     * @return DataSign签名
     */
    private function encrypt($data, $appkey) {
        return urlencode(base64_encode(md5($data.$appkey)));
    }

    public function sendPrintOrderRequest()
    {

    }

    /**
     * 组装POST表单用于调用快递鸟批量打印接口页面
     * @param $config
     */
    function buildForm(array $config) {
        //OrderCode:需要打印的订单号，和调用快递鸟电子面单的订单号一致，
        //PortName：本地打印机名称，请参考使用手册设置打印机名称。支持多打印机同时打印。
        //$request_data = '[{"OrderCode":"234351215333113311353","PortName":"打印机名称一"},{"OrderCode":"234351215333113311354","PortName":"打印机名称二"}]';
        $request_data = json_encode($config);
        $request_data_encode = urlencode($request_data);
        $data_sign = $this->encrypt(request()->getClientIp().$request_data_encode, APIKey);
        //是否预览，0-不预览 1-预览
        $is_priview = '0';

        //组装表单
        $form = '<form id="form1" method="POST" action="'.API_URL.'"><input type="text" name="RequestData" value="'.$request_data.'"/><input type="text" name="EBusinessID" value="'.EBusinessID.'"/><input type="text" name="DataSign" value="'.$data_sign.'"/><input type="text" name="IsPriview" value="'.$is_priview.'"/></form><script>form1.submit();</script>';
        return $form;
    }
}
